package com.example.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	public static String YYYY_MM_DD = "yyyy-MM-dd";
	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	private static String[] parsePatterns = {
	            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
	            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
	            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
	/**
	 * 比较时间字符串大小，dateStr1 > dateStr2 :1  dateStr1 < dateStr2 :-1  dateStr1 == dateStr2 :0
	 * @param dateStr1 时间字符串 yyyy-MM-dd格式
	 * @param dateStr2 时间字符串 yyyy-MM-dd格式
	 * @return 0,1,-1
	 */
	
	public static int compare2DateString(String dateStr1,String dateStr2) {
		int i = 0;
		try {
			i = new SimpleDateFormat(YYYY_MM_DD).parse(dateStr1).compareTo(new SimpleDateFormat(YYYY_MM_DD).parse(dateStr2));
		} catch (ParseException e) {
			log.error("时间字符串转换异常时间1：{},时间2：{}",dateStr1,dateStr2);
			e.printStackTrace();
		}
        return i;
	}
	
	/**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDateStr(){
        return dateTimeNow(YYYY_MM_DD);
    }
    /**
     * 获取当前时间, 默认格式为yyyy-MM-dd HH:mm:ss
     * 
     * @return String
     */
    public static String getTimeStr(){
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow(final String format){
        return parseDateToStr(format, new Date());
    }
    
    public static final String parseDateToStr(final String format, final Date date){
        return new SimpleDateFormat(format).format(date);
    }
    
    public static final int getCurrentYear() {
    	return Calendar.getInstance().get(Calendar.YEAR);
    }
    /**
     * 第一季度最后一天
     * @return
     */
    public static final String getQ1thDateStr() {
    	return getCurrentYear()+"-03-31";
    }
    /**
     * 第二季度最后一天
     * @return
     */
	public static final String getQ2ndDateStr() {
		return getCurrentYear()+"-06-30";
	}
	/**
     * 第三季度最后一天
     * @return
     */
	public static final String getQ3rdDateStr() {
		return getCurrentYear()+"-09-30";
	}
	/**
     * 第四季度最后一天
     * @return
     */
	public static final String getQ4thDateStr() {
		return getCurrentYear()+"-12-31";
	}
	
	/**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
    
    /**
     * 取年月的最大天数
     * @param year
     * @param month
     * @return
     */
    public static int getMonthMaxDays(String year,String month) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(Integer.parseInt(year), Integer.parseInt(month), 0);
//    	System.out.println(calendar.get(Calendar.YEAR));
//    	System.out.println(calendar.get(Calendar.MONTH)+1);
//    	System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    	return calendar.get(Calendar.DAY_OF_MONTH);
    }
    /**
     * 获取月份第一天
     * @param year
     * @param month
     * @return
     */
    public static String getMonthFirstDay(String year,String month) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
//    	System.out.println(calendar.get(Calendar.YEAR));
//    	System.out.println(calendar.get(Calendar.MONTH)+1);
//    	System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    	if(Integer.parseInt(month)<10 && month.length() == 1) {
    		month = "0" + month;
    	}
    	return calendar.get(Calendar.YEAR)+"-"+month+"-01";
    }
    /**
     * 获取月份最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getMonthLastDay(String year,String month) {
    	Calendar calendar = Calendar.getInstance();
    	calendar.set(Integer.parseInt(year), Integer.parseInt(month), 0);
//    	System.out.println(calendar.get(Calendar.YEAR));
//    	System.out.println(calendar.get(Calendar.MONTH)+1);
//    	System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
    	if(Integer.parseInt(month)<10 && month.length() == 1) {
    		month = "0" + month;
    	}
    	
    	int day = calendar.get(Calendar.DAY_OF_MONTH);
    	String dayStr = "" + day;
    	if(day<10) {
    		dayStr = "0" + day;
    	}
    	return calendar.get(Calendar.YEAR)+"-"+month+"-"+dayStr;
    }
    
    public static void main(String[] args) {
    	System.out.println(getMonthFirstDay("2021","2"));
    	System.out.println(getMonthLastDay("2021","2"));
	}
}
